Imports System.Collections.Generic
Imports System.Text

Namespace Core
	<DbfDotNet.Record(FieldMapping := FieldMapping.ExplicitColumnsOnly)> _
	Friend MustInherit Class SortFields
		Friend Sub Read(ndxFile As NdxFile, dbfRecord As Object)
			Throw New NotImplementedException()
		End Sub

		Friend Sub Read(ndxFile As NdxFile, recordBuffer As [Byte]())
			Throw New NotImplementedException()
		End Sub

		Protected MustOverride Function ChainCompareIsEqual(other As SortFields, result As Integer()) As Boolean

		Public Function ChainCompare(other As SortFields, result As Integer()) As Boolean
			Return ChainCompareIsEqual(other, result)
		End Function

		Friend Function Clone() As SortFields
			Dim result As SortFields = DirectCast(MyBase.MemberwiseClone(), SortFields)
			Return result
		End Function
	End Class
    Friend Class SortFields(Of T1 As IComparable(Of T1))
        Inherits SortFields
        Public f1 As T1

        Protected Overrides Function ChainCompareIsEqual(other As SortFields, result As Integer()) As Boolean
            Dim cmp As Integer = f1.CompareTo(DirectCast(other, SortFields(Of T1)).f1)
            result(0) = cmp
            Return cmp = 0
        End Function

        Public Overrides Function ToString() As String
            Return f1.ToString()
        End Function
    End Class

	Friend Class SortFields(Of T1 As IComparable(Of T1), T2 As IComparable(Of T2))
		Inherits SortFields(Of T1)
		Public f2 As T2

		Protected Overrides Function ChainCompareIsEqual(other As SortFields, result As Integer()) As Boolean
			If MyBase.ChainCompareIsEqual(other, result) Then
				Dim cmp As Integer = f2.CompareTo(DirectCast(other, SortFields(Of T1, T2)).f2)
				result(1) = cmp
				Return cmp = 0
			End If
			Return False
		End Function
	End Class

	' that's enough for now (but we should extend to at least 4 or 5)
End Namespace
